package com.whfc.wxmp.api.sys.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.dto.WxInviteCodeDTO;
import com.whfc.fuum.dto.WxInviteDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.param.WxInviteUserJoinParam;
import com.whfc.wxmp.api.sys.service.WxInviteService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 邀请用户
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年11月11日
 */
@RestController
@RequestMapping("/mp/api/invite")
public class WxInviteController extends BaseController {

    @Autowired
    private WxInviteService wxInviteService;

    /**
     * 获取项目的邀请码
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/code")
    public Result getCode(@CurrentWxUser WxUser user, @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取项目的邀请码", "{\"deptId\":\"" + deptId + "\"}");
        WxInviteCodeDTO wxInviteCodeDTO = wxInviteService.getCode(user, deptId);
        return ResultUtil.success(wxInviteCodeDTO);
    }

    /**
     * 根据邀请码获取项目信息
     *
     * @param user
     * @param code
     * @return
     */
    @GetMapping("/judge")
    public Result judge(@CurrentWxUser WxUser user, @RequestParam("code") String code) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "根据邀请码获取项目信息", "{\"code\":\"" + code + "\"}");
        WxInviteDTO wxProjectDTO = wxInviteService.getInviteInfo(user.getPhone(), code);
        return ResultUtil.success(wxProjectDTO);
    }

    /**
     * 通过邀请码进入项目
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/user/join")
    public Result inviteUserJoin(@CurrentWxUser WxUser user, @Validated @RequestBody WxInviteUserJoinParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "通过邀请码进入项目", JSONUtil.toString(request));
        wxInviteService.inviteUserJoin(user, request);
        return ResultUtil.success();
    }
}
